<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use Yii;

/**
 * This is the model class for table "{{%activity}}".
 *
 * @property integer $id
 * @property string $title
 * @property string $description
 * @property string $type
 * @property string $conditions
 * @property integer $stop_rules_processing
 * @property string $action_solution
 * @property integer $start_time
 * @property integer $end_time
 * @property integer $status
 * @property integer $created_at
 */
class Activity extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%activity}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['description'], 'string'],
            [['stop_rules_processing','status', 'created_at','is_delete','priority'], 'integer'],
            [['title'],'string', 'max' => 100],
            ['title','unique','message'=>'此活动已存在！'],
            [['title','start_time','end_time','stop_rules_processing','status','conditions','action_solution','priority'], 'required'],
            [['type'], 'string', 'max' => 20],
            [['conditions', 'action_solution'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'title' => Yii::t('zhtfrontend', '活动标题'),
            'description' => Yii::t('zhtfrontend', '活动描述'),
            'type' => Yii::t('zhtfrontend', '活动类型'),
            'conditions' => Yii::t('zhtfrontend', '活动条件'),
            'stop_rules_processing' => Yii::t('zhtfrontend', '是否排斥'),
            'action_solution' => Yii::t('zhtfrontend', '活动方案'),
            'priority' => Yii::t('zhtfrontend', '优先级'),
            'start_time' => Yii::t('zhtfrontend', '开始时间'),
            'end_time' => Yii::t('zhtfrontend', '结束时间'),
            'status' => Yii::t('zhtfrontend', '是否启用'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'is_delete' => Yii::t('zhtfrontend', '是否删除'),
            'priority' => Yii::t('zhtfrontend', '优先级'),

        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['start_time'] = function () {
            return \Yii::$app->formatter->asDatetime($this->start_time);
        };
        $fields['end_time'] = function () {
            return \Yii::$app->formatter->asDatetime($this->end_time);
        };
        $fields['stop_rules_processing'] = function () {
            switch($this->stop_rules_processing){
                case 0:
                    return '是';
                case 1:
                    return '否';
            }
        };

        return $fields;
    }

    public function extraFields()
    {
        return [
            'status_name'=> function(){
                switch ($this->status) {
                    case 0:
                        return \Yii::t('zhtfrontend', '未启用');
                    case 1:
                        return \Yii::t('zhtfrontend', '已启用');
                }
            },
            'operate' => function(){
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            }
        ];
    }
}
